/*
 * Copyright 2021 UltrasonicMadness
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Bubble
{
    private final int[] COLORS = {0x80FF0000, 0x80FFFF00, 0x8000FF00, 0x8000FFFF, 0x800000FF, 0x80FF00FF};
  
    // Color and X and Y position of bubble
    private int xPos;
    private int yPos;
    private int bubbleColor;
  
    // X offset, max X offset and whether bubble is moving to the right
    private int xOffset;
    private int xOffsetMax;
    private boolean movingRight; 
  
    // Size, speed and whether bubble is active
    private int size;
    private int speed;
    private boolean active;
  
    public Bubble(int initXPos, int initYPos, int initSize, boolean bubbleTeaMode)
    {
        xPos = initXPos;
        yPos = initYPos + initSize;
        updateColor(bubbleTeaMode);
        
        size = initSize;
        active = false;
    
        xOffset = 0;
        xOffsetMax = int(random(3,11));
        
        // Randomly true or false
        movingRight = (int(random(0,1)) == 1);
    
        // The speed is between a quarter and a half of the bubble's size
        speed = int(random(initSize * 0.25, initSize * 0.5));
    }
    
    public void draw()
    {
        fill(bubbleColor);
        stroke(0,0,0,128);
        ellipse(xPos + xOffset, yPos, size, size);
    }
  
    public void advance()
    {
        if (active)
        {
            // Move bubble up
            yPos = yPos - speed;
    
            // If X offset is over (or under) a certain value, swap direction
            if (xOffset <= -(xOffsetMax))
            {
                movingRight = true;
            }
            else if (xOffset >= xOffsetMax)
            {
                movingRight = false;
            }
      
            // Move bubble 1-3 pixels left or right
            if (movingRight)
            {
                xOffset += int(random(1,3));
            }
            else
            {
                xOffset -= int(random(1,3));
            }
    
            // Deactivate bubble if it is off screen
            if (yPos < -size)
            {
                active = false;
            }
        }
    }
  
    public void activate(boolean bubbleTeaMode)
    {
        // If this bubble is not currently active.
        if (!active)
        {
            active = true;
    
            xPos = int(random(-size, width + size));
            yPos = height + size;
            updateColor(bubbleTeaMode);
        }
    }
    
    private void updateColor(boolean bubbleTeaMode)
    {
        if (bubbleTeaMode)
        {
            bubbleColor = COLORS[int(random(0, COLORS.length))];
        }
        else
        {
            // The non-rainbow color
            bubbleColor = 0x80C4C4FF;
        }
    }
}
